<?php
namespace app\common\model;

class DeptModel extends BaseModel
{
    protected $name = 'dept'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;

    /**
     * 获取树型列表
     */
    private function getDeptList()
    {
        $dept = $this->order('dept_sort')->select()->toArray();
        $dept = \org\util\TreeUtil::tree($dept, 'dept_name', 'dept_id', 'dept_fid');
        return $dept;
    }

    /**
     * 更新缓存
     */
    public function updateCache()
    {
        try{
            $dept  = $this->getDeptList();
            $data      = [];
            foreach ($dept as $v) {
                $isSon            = \org\util\TreeUtil::hasChild($dept, $v['dept_id'], 'dept_fid');
                $v['is_son']      = $isSon;
                $data[$v['dept_id']]  = $v;
            }
            cache('dept', $data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

}
